package ru.edu.vstu.atpp.acm.ui.pages.management;

import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.beaneditor.BeanModel;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.BeanModelSource;
import ru.edu.vstu.atpp.acm.annotation.AccessRights;
import ru.edu.vstu.atpp.acm.annotation.LoggedInUserAccess;
import ru.edu.vstu.atpp.acm.ui.entities.Groups;
import ru.edu.vstu.atpp.acm.ui.services.dao.GroupsDAO;

import java.util.List;
import java.util.Locale;

@LoggedInUserAccess
@AccessRights(group = ru.edu.vstu.atpp.acm.ui.security.AccessRight.MANAGE_SYSTEM)
public class GroupManagementPage {

    @Inject
    private GroupsDAO groupsDAO;

    @Property
    private Groups group;

    @Property
    private List<Groups> groupsList;

    @Property
    private BeanModel<Groups> groupsModel;

    @Inject
    private BeanModelSource beanModelSource;

    @Inject
    private Messages messages;

    @Inject
    private Locale locale;

    @SetupRender
    private void onSetupRender() {
        groupsModel = beanModelSource.createDisplayModel(Groups.class, messages);
        groupsModel.include(Groups.ID_FIELD);
        groupsModel.add("name", null);
        groupsList = groupsDAO.getAll();
    }

    public String getGroupName() {
        return group.getMultilingualId().getLocalizedString(locale);
    }

}